var app = getApp()
var maxPageIndex = 0
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ifShownoData: true,
    pageIndex: 1,
    result: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.accountInfo()
  },

  //对账单信息
  accountInfo: function() {
    var that = this
    wx.showLoading({
      title: '拼命加载中',
      mask: true
    })
    wx.request({
      method: 'POST',
      url: app.preUrl + 'accountList',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId,
        page: that.data.pageIndex,
        size: 8
      },
      success: function(res) {
        console.log(res.data.result)
        wx.hideLoading()
        if (res.data.code == 100) {
          let data = res.data.result.result
          maxPageIndex = res.data.total / 8
          if (data.length > 0) {
            that.setData({ ifShownoData: true })
          } else {
            that.setData({ ifShownoData: false })
          }
          if (that.data.pageIndex != 1) {
            let array = that.data.result.concat(data)
            that.setData({ result: array })
          } else {
            that.setData({ result: data })
          }
        } else if (res.data.code == 400) {
          that.setData({ ifShownoData: false, result: '' })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            confirmColor: '#56BDE0',
            showCancel: false
          })
        }
      },
      fail: function(error) {
        console.log(error)
      }
    })
  },
  goDetail(e) {
    let data = e.currentTarget.dataset.item
    wx.navigateTo({ url: 'myBalance_detail?data=' + JSON.stringify(data) })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this
    that.setData({
      pageIndex: 1, // 每次触发下拉事件pageIndex=1
      result: ''
    })
    that.accountInfo()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    if (maxPageIndex <= that.data.pageIndex) {
      return
    }
    that.setData({
      pageIndex: that.data.pageIndex + 1
    })
    // 上拉获取更多数据
    that.accountInfo()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
})
